//
//  Time.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/9.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

extension String
{
    func timeStampToString(timeStamp:String)->String {
        
        let cal = NSCalendar.current
        let dfmatter = DateFormatter()
        dfmatter.dateFormat="EEE MMM dd HH:mm:ss ZZZZ yyyy"
        dfmatter.locale = Locale(identifier: "en-US")
        let da = dfmatter.date(from: timeStamp)
  
        if cal.isDateInToday(da!)
        {
        var timeInterVal = -(da?.timeIntervalSinceNow)!
        
            if timeInterVal < 60
            {
            return "刚刚"
            
            }
        
            if  timeInterVal < 3600 {
               
                timeInterVal = timeInterVal / 60
                return  "\(Int(timeInterVal))分钟前"
            }
            
            timeInterVal = timeInterVal / 3600
            return "\(Int(timeInterVal))小时前"
        }
        let str = "\(da!)".replacingOccurrences(of: "+0000", with: "")
       
        
        return str;
    }
}
